package com.x.reader.service;

import com.x.framework.Base;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import com.x.reader.dao.VideoDao;
import com.x.reader.model.Video;
import com.x.reader.model.VideoChapter;
import org.directwebremoting.io.FileTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class VideoService extends BaseService {

    @Autowired
    private VideoDao videoDao;

    public Video insertVideo(Video video, FileTransfer videoCoverFile) {
        try {
            String videoId = this.getUid(32);
            String videoCover = videoCoverFile.getFilename();
            videoCover = videoId + videoCover.substring(videoCover.lastIndexOf(".")).toLowerCase();
            video.setVideoId(videoId);
            video.setVideoCover(videoCover);
            video.setVideoCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            video = this.insert(video);
            this.uploadFile(videoCoverFile, "/upload/video/cover", videoCover);
            return video;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void updateVideo(Video video, FileTransfer videoCoverFile) {
        try {
            String videoCover = null;
            if (Base.isNotNull(videoCoverFile)) {
                String videoId = video.getVideoId();
                videoCover = videoCoverFile.getFilename();
                videoCover = videoId + videoCover.substring(videoCover.lastIndexOf(".")).toLowerCase();
                video.setVideoCover(videoCover);
            }
            video.setVideoCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            this.update(video);
            if (Base.isNotNull(videoCover)) {
                this.deleteFile("/upload/video/cover", videoCover);
                this.uploadFile(videoCoverFile, "/upload/video/cover", videoCover);
            }
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteVideo(Video video) {
        try {
            if (Base.isNotNull(video.getVideoCover())) {
                this.deleteFile("/upload/video/cover", video.getVideoCover());
            }
            Video v = new Video();
            v.setVideoId(video.getVideoId());
            this.delete(v);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Video> selectVideoList(Video video) {
        try {
            return videoDao.selectVideoList(video);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public int selectVideoNum(Video video) {
        try {
            return videoDao.selectVideoNum(video);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public VideoChapter insertVideoChapter(VideoChapter videoChapter, FileTransfer videoChapterFileFile) {
        try {
            String videoChapterId = this.getUid(32);
            String videoChapterFile = videoChapterFileFile.getFilename();
            videoChapterFile = videoChapterId + videoChapterFile.substring(videoChapterFile.lastIndexOf(".")).toLowerCase();
            videoChapter.setVideoChapterId(videoChapterId);
            videoChapter.setVideoChapterFile(videoChapterFile);
            videoChapter.setVideoChapterCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            videoChapter = this.insert(videoChapter);
            this.uploadFile(videoChapterFileFile, "/upload/video/file", videoChapterFile);
            return videoChapter;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void updateVideoChapter(VideoChapter videoChapter, FileTransfer videoChapterFileFile) {
        try {
            String videoChapterFile = null;
            if (Base.isNotNull(videoChapterFileFile)) {
                String videoChapterId = videoChapter.getVideoChapterId();
                videoChapterFile = videoChapterFileFile.getFilename();
                videoChapterFile = videoChapterId + videoChapterFile.substring(videoChapterFile.lastIndexOf(".")).toLowerCase();
                videoChapter.setVideoChapterFile(videoChapterFile);
            }
            videoChapter.setVideoChapterCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            this.update(videoChapter);
            if (Base.isNotNull(videoChapterFile)) {
                this.deleteFile("/upload/video/file", videoChapterFile);
                this.uploadFile(videoChapterFileFile, "/upload/video/file", videoChapterFile);
            }
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteVideoChapter(VideoChapter videoChapter) {
        try {
            if (Base.isNotNull(videoChapter.getVideoChapterFile())) {
                this.deleteFile("/upload/video/file", videoChapter.getVideoChapterFile());
            }
            VideoChapter v = new VideoChapter();
            v.setVideoChapterId(videoChapter.getVideoChapterId());
            this.delete(v);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<VideoChapter> selectVideoChapterList(VideoChapter videoChapter) {
        try {
            return videoDao.selectVideoChapterList(videoChapter);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }
}
